//
//  CGSettingViewController.m
//  SchoolClient
//
//  Created by mac on 15/10/28.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGSettingViewController.h"
#import "AppDelegate.h"
@interface CGSettingViewController ()
//@property(nonatomic,strong)UISwitch *mySwitch;
@end

@implementation CGSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"设置";
    
    [self initTableView];
    //使cell的线条充满
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }

}


-(void)initTableView{
    
    CGRect aRect = CGRectMake(0,
                              0,
                              SCREEN_WIDTH,
                              SCREEN_HEIGHT-64.0);
    self.tableView = [[UITableView alloc] initWithFrame:aRect  style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.scrollEnabled = NO;
    [self.view addSubview: self.tableView];
    
    //在底部添加一退出铵钮
    UIButton *quitButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 224, SCREEN_WIDTH - 40, 44)];
    [quitButton setBackgroundImage:[UIImage imageNamed:@"mine_btn_bg"] forState:UIControlStateNormal];
    [quitButton setTitle:@"退出当前账号" forState:UIControlStateNormal];
    [self.view addSubview:quitButton];
    [quitButton addTarget:self action:@selector(clickQuitButton) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 退出当前账号
-(void)clickQuitButton{
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"主人,您还没玩够呢" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"狠心退出", nil];
    [alertView show];

}


#pragma mark 重写表格的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"tableviewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
    }
    
    
    switch (indexPath.row) {
        case 0:
        {
            cell.textLabel.text = @"自动登录";
            //添加一个开关
            UISwitch *switchView = [[UISwitch alloc] init];
            [switchView setOn:YES animated:YES];
           CGAccountDefault *user = [CGAccountDefault shareAccount];
            
            NSLog(@"***** %hhd",user.isAutoLogin);
            if (user.isAutoLogin) {
                switchView.on = YES;
            }else{
                switchView.on = NO;
            }
         
            switchView.onTintColor = [UIColor colorWithRed:60/255.0 green:174/255.0 blue:224/255.0 alpha:1];
            [switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
            cell.accessoryView = switchView;
            //self.mySwitch = switchView;
            break;
        }
        case 1:
            cell.textLabel.text = @"更换手机号";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        case 2:
            cell.textLabel.text = @"修改密码";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
//        default:
//            cell.textLabel.text = @"当前版本号:";
//            //添加一个Label
//            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(110, 0, 100, 44)];
//            label.text = [CGAccountDefault shareAccount].version;
//            [cell.contentView addSubview:label];
//            break;
            
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CGAccountDefault *user = [CGAccountDefault shareAccount];
    if (indexPath.row == 1) {
        
        if (user.login) {
            CGModifyPhoneViewController *modifyPhoneVC = [CGModifyPhoneViewController new];
            [self.navigationController pushViewController:modifyPhoneVC animated:YES];
        }else{
            CGLoginViewController *loginVC = [CGLoginViewController new];
            [self.navigationController pushViewController:loginVC animated:YES];
        }
   
    }else if (indexPath.row == 2){
        
        if (user.login) {
            CGModifyPasswordViewController *modifyPasswordVC = [CGModifyPasswordViewController new];
            [self.navigationController pushViewController:modifyPasswordVC animated:YES];
        }else{
            CGLoginViewController *loginVC = [CGLoginViewController new];
            [self.navigationController pushViewController:loginVC animated:YES];
        }
    }
}


//设置表头表尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 15;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(void)switchAction:(UISwitch *)sender{
    
    CGAccountDefault *user = [CGAccountDefault shareAccount];
    if (!user.login) {
        return;
    }
    if ([sender isOn]) {
        //发送请求
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"account"] = user.account;
        parameters[@"password"] = user.password;
        
        ZYHRequest *request = [ZYHRequest new];
        [request postAsyncEncryptRequest:@"/main/AUTOlogin.app" parameters:parameters success:^(id responseData) {
            
            NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
            if ([result rangeOfString:@"account"].location != NSNotFound) {
                sender.on = YES;
                user.isAutoLogin = YES;
            }else{
                sender.on = NO;
                user.isAutoLogin = NO;
                [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
               
            }
            [user saveToSandBox];
            
        } failure:^(NSError *error) {
            sender.on = NO;
            [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
            return;
        }];
        
    }else{
        user.isAutoLogin = NO;
        [user saveToSandBox];
    }
    
}

#pragma mark alertView代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        //先把登录标记设为NO
        CGAccountDefault *account = [CGAccountDefault shareAccount];
        account.login = NO;
        account.account = nil;
        account.password = nil;
        account.tphone = nil;
        account.isAutoLogin = YES;
        account.headerIconPath = nil;
        account.isVip = NO;
        [account saveToSandBox];
        
        //返回主界面
        CGDataCenter *center = [CGDataCenter sharedDataCenter];
        center.headerIcon = nil;
        [self.navigationController popViewControllerAnimated:NO];
        AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        [app.tabBarController setSelectedIndex:0];
        keyWindow.rootViewController = app.tabBarController;
    }
}

//使cell的线条充满
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}

@end
